<?php
/**
 * Created by PhpStorm.
 * User: nmw
 * Date: 17-10-13
 * Time: 下午4:08
 */

namespace Model;

use Think\Model;

class CoreModel extends Model
{
    /**
     * 按条件获取单条记录
     *
     * @param $table
     * @param $select
     * @param array $params
     * @return bool|mixed
     */
    function getItem($table, $select, $params = [])
    {
        $data = FALSE;

        if (empty($data))
        {
            $data = M($table)->field($select)->where($params)->find();
        }

        return $data;
    }

    /**
     * 按条件获取多条记录
     *
     * @param $table
     * @param $select
     * @param array $params
     * @return array|mixed
     */
    function getItems($table, $select, $params = [])
    {
        $data = [];

        if (empty($data))
        {
            $data = M($table)->field($select)->where($params)->select();
        }

        return $data;
    }

    /**
     * 按条件获取数量
     *
     * @param $table
     * @param $params
     * @return int|mixed
     */
    function getTotals($table, $params)
    {
        $data = 0;

        if (empty($data))
        {
            $totals = M($table)->where($params)->getField('count(1) totals');
            $data = $totals;
        }

        return $data;
    }

    /**
     * 按条件获取多条记录并按键名索引
     *
     * @param $table
     * @param $indexKey
     * @param string $select
     * @param array $params
     * @return array
     */
    function getCates($table, $indexKey, $select = '', $params = [])
    {
        $data = [];

        if (empty($data))
        {
            $lists = M($table)->field($select)->where($params)->select();

            foreach ($lists as $item)
            {
                $data[$item[$indexKey]] = $item;
            }
        }

        return $data;
    }

    /**
     * 按条件获取内容列表
     *
     * @param $table
     * @param $select
     * @param array $params
     * @param int $page
     * @param int $length
     * @return array
     */
    function getList($table, $select, $params = [], $page = 1, $length = 10)
    {
        $data = [];

        if (empty($data))
        {
            $total = $this->getTotals($table, $params);
            $data['total'] = $total;
            $offset = $page * $length;
            $data['lists'] = M($table)->field($select)->where($params)->limit($offset, $length)->select();
        }

        return $data;
    }

    /**
     * 插入数据，存在则更新
     *
     * @param $table
     * @param $data
     * @return mixed
     */
    function insert($table, $data)
    {
        return M($table)->add($data, [], $data);
    }

    /**
     * 批量插入数据
     *
     * @param $table
     * @param $datas
     * @return bool|string
     */
    function insertBatch($table, $datas)
    {
        return M($table)->addAll($datas, [], $datas);
    }

    /**
     * 更新数据
     *
     * @param $table
     * @param $data
     * @param $where
     * @return bool
     */
    function update($table, $data, $where)
    {
        return M($table)->where($where)->save($data);
    }

    /**
     * 删除数据
     *
     * @param $table
     * @param $where
     * @return mixed
     */
    function deleteItem($table, $where)
    {
        return M($table)->where($where)->delete();
    }
}